package com.dongdongmovie.service.impl;

import com.dongdongmovie.mapper.VipUserMapper;
import com.dongdongmovie.pojo.VipUser;
import com.dongdongmovie.pojo.VipUserExample;
import com.dongdongmovie.service.VipUser_Service;
import com.dongdongmovie.util.MD5Utils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@DubboService
public class VipUser_ServiceImpl implements VipUser_Service {

    //注入mapper
    @Autowired
    private VipUserMapper vipUserMapper;

    //注册
    @Override
    public int add(VipUser vipUser) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        VipUserExample ve = new VipUserExample();
        VipUserExample.Criteria criteria = ve.createCriteria();
        criteria.andUsernameEqualTo(vipUser.getUsername());
        List<VipUser> vipUsers = vipUserMapper.selectByExample(ve);
        if (vipUsers.size() != 0) {
            return 0;
        }
        vipUser.setPassword(MD5Utils.getEncryptedPwd(vipUser.getPassword()));
        vipUserMapper.insertSelective(vipUser);
        return 1;
    }

    //登录
    @Override
    public int show(VipUser vipUser) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        VipUserExample ve = new VipUserExample();
        VipUserExample.Criteria criteria = ve.createCriteria();
        criteria.andUsernameEqualTo(vipUser.getUsername());
        List<VipUser> vipUsers = vipUserMapper.selectByExample(ve);
        if (vipUsers.size() == 0) {
            return 0;
        }
        if (MD5Utils.validPassword(vipUser.getPassword(),vipUsers.get(0).getPassword())) {
            return 2;
        }
        return 1;
    }

    //查询登录会员信息
    @Override
    public VipUser showuserid(String username) {
        VipUserExample ve = new VipUserExample();
        VipUserExample.Criteria criteria = ve.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<VipUser> vipUsers = vipUserMapper.selectByExample(ve);
        VipUser vipUser = vipUsers.get(0);
        return vipUser;
    }
}
